package frontend.components.language;

import java.util.Locale;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.resource.ContextRelativeResource;

public class LanguagePanel extends Panel {
    public LanguagePanel(String id) {
        super(id);

        Form languageForm = new Form("languageForm");

        languageForm.add(createLocaleChangingButton("roButton", "images/Romania-Flag.png", "RO"));
        languageForm.add(createLocaleChangingButton("enButton", "images/United-Kingdom-Flag.png", "EN"));

        add(languageForm);
    }

    private Button createLocaleChangingButton(final String buttonId, final String imagePath, final String localeString) {
        return new ImageButton(buttonId, new ContextRelativeResource(imagePath)) {
            @Override
            public void onSubmit() {
                changeUserLocaleTo(localeString);
            }
        };
    }

    private void changeUserLocaleTo(String localeString) {
        getSession().setLocale(new Locale(localeString));
    }
}
